#ifndef GAME_MATH_H
#define GAME_MATH_H

#include "hge.h"
//#ifndef M_PI
//#define M_PI (3,1415926535897932384)
//#endif

#define NEG_FLOAT(x) (*((DWORD *)&(x))) ^= 0x80000000
#define MAKE_ABS_FLOAT(x) (*((DWORD *)&(x))) &= 0x7FFFFFFF
#define MAKE_ABS_DOUBLE(x) (*(((DWORD *)&(x)) + 1)) &= 0x7FFFFFFF
#define COPY_SIGN_FLOAT(to, from) { (*((DWORD *)&(to))) = ((*((DWORD *)&(to)))&0x7FFFFFFF) | ((*((DWORD *)&(from))) & 0x80000000); }
#define SET_SIGN_FLOAT(to, sign) {*((DWORD *)&(to)) = ((*((DWORD *)&(to)))&0x7FFFFFFF) | ((DWORD(sign)) << 31); }

#define degtorad(dir) ((dir) * M_PI / 180)
#define radtodeg(dir) ((dir) * 180 / M_PI)

#include <cmath>

class Gamemath
{
	public:
		static inline float point_distance(float x1, float y1, float x2, float y2){return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));}
		//float point_direction(float x1, float y1, float x2, float y2);
		static inline float point_direction(float x1, float y1, float x2, float y2)
		{
			float dX = x2 - x1;
			float dY = y2 - y1;

			float dist = sqrt((dX*dX) + (dY*dY));
			float dXa = abs(dX);

			float beta = radtodeg(acos(dXa / dist));

			float angle;

			if(dX > 0)
			{
				if (dY < 0)
    				angle = 270 + beta;
				else
    				angle = 270 - beta;
			}
			else
			{
				if (dY < 0)
    				angle = 90 - beta;
				else
    				angle = 90 + beta;
			}
			return (360 - fmodf(angle + 180, 360));
		}

		static inline int dtoi(double d)
		{
		  const double t = d + 6755399441055744.0;
		  return *((int *)(&t));
		}

		static inline int roundf (float x) 
		{
		  //int n;
		  /*asm {
			fld     x
			fistp   n
		  }*/
		  return -1;
		}

		static float direction_difference(float x1, float y1, float x2, float y2);
		static inline float lengthdir_x(float len, float dir) {return( sin(degtorad(dir)) * len);}
		static inline float lengthdir_y(float len, float dir) {return(-cos(degtorad(dir)) * len);}
		
		static inline int sign(double v){return v > 0 ? 1 : (v < 0 ? -1 : 0);}
		static inline int sign(float v){return v > 0 ? 1 : (v < 0 ? -1 : 0);}
		static inline int sign(int v) {return v > 0 ? 1 : (v < 0 ? -1 : 0);}

	private:
};

#endif

